package com.haixue.android.academy.activity;

import android.content.Intent;

import com.haixue.android.academy.MyApplication;
import com.haixue.android.academy.domain.GoodsWapper;
import com.haixue.android.academy.domain.User;
import com.haixue.android.academy.domain.UserWapper;
import com.haixue.android.academy.utils.IntentUtils;
import com.haixue.android.academy.utils.Url;

import org.apache.http.Header;

import cn.woblog.android.common.utils.TimeUtils;
import cn.woblog.android.common.utils.ToastAlone;
import cn.woblog.android.http.Http;
import cn.woblog.android.http.JsonHandler;
import cn.woblog.android.http.RequestParams;

/**
 * Created by ren on 2015/5/22 0022.
 */
public abstract class BaseLoginActivity extends  BaseActivity implements TimeUtils.OnDelayedSuccess {
    protected static final int TO_HOME_ACTIVITY = 1;
//    protected UserWapper data;
    public void toLoginActivity() {
        IntentUtils.toActivityAfterFinish(getContext(), LoginActivity.class);
    }

    public void doLogin(final String username, final String password) {
        TimeUtils.beginTime();
        RequestParams params = myParams.getLoginRequestParams();
        params.put("user", username);
        params.put("password", password);
             Http.post(Url.getURL(Url.USER_LOGIN_URL), myParams.genertorReuqestParams(params), new JsonHandler<UserWapper>() {


                 @Override
                 protected void fail(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                     log.debug("login fail " + throwable.getMessage());
                 }

                 @Override
                 protected void success(int statusCode, Header[] headers, UserWapper data) {
                     if (data != null && data.getS() == Http.OK) {
                         log.debug("login success " + data.getData().getNickName());
//                     testArrayJsonObject(data.getData());
                         MyApplication.setUser(data.getData());
                         sp.setUserInfo(username + "," + password);
                         if (3000 > TimeUtils.endTime()) {
                             log.debug("to home activity delayed " + (3000 > TimeUtils.endTime()));
                             TimeUtils.delayed(3000 - TimeUtils.endTime(), BaseLoginActivity.this, TO_HOME_ACTIVITY);
                         } else {
                             log.debug("to home activity no delayed");
                             toHomeActivity();
                         }
                     } else {
                         if (data != null && data.getM() != null) {
                             log.debug("login fail " + data.getM());
                             ToastAlone.showLongToast(getActivity(), data.getM());
                         } else {
                             log.debug("login fail ");
                             ToastAlone.showLongToast(getActivity(), data.getM());
                         }
                         IntentUtils.toActivityAfterFinish(getActivity(), LoginActivity.class);
                     }
                 }

             }.cache("a"));
    }

    private void testArrayJsonObject(User data) {
        RequestParams datas = myParams.getDefaultRquestParams();
        datas.put("uid",data.getUid()+"");
        datas.put("sk",data.getSk());
        datas.put("categoryIds",data.getCategory().get(0).getId()+"");
        Http.get(Url.getURL(Url.GET_GOODS_URL), myParams.genertorReuqestParams(datas), new JsonHandler<GoodsWapper>() {


            @Override
            protected void fail(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                log.debug("get goods faild " + throwable.getMessage());
            }

            @Override
            protected void success(int statusCode, Header[] headers, GoodsWapper data) {
                if (data != null && data.getData() != null) {
                    log.debug("get goods success " + data.getData().get(0).getGoods().size());
                } else {
                    log.debug("get goods faild");
                }
            }
        });
    }

    @Override
    public void onDelayedSuccess(int flag) {

    }

    protected void toHomeActivity() {
        Intent intent = new Intent(this, HomeActivity.class);
        intent.putExtra("data",MyApplication.getUser());
        IntentUtils.toActivityAfterFinish(this, intent);
    }
}
